﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Mal.Balades.Admin.Controls
{
    public class HtmlTextEditor : Control
    {
        #region Fields
        private string textValue;
        #endregion

        #region Construction / Destruction
        public HtmlTextEditor()
        {
            this.DefaultStyleKey = typeof(HtmlTextEditor);
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {

                SetValue(TextProperty, value);
            }
        }

        public static readonly DependencyProperty TextProperty
         = DependencyProperty.Register(
                                   "Text", typeof(string),
                                   typeof(HtmlTextEditor),
                                   new PropertyMetadata(null, TextChanged)
                                 );       

        private Button Paragraph
        {
            get
            {
                Button control = this.GetTemplateChild("PART_Paragraph") as Button;
                //Debug.Assert(control != null, "PART_Paragraph is not found the Template");
                return control;
            }
        }

        private Button Bold
        {
            get
            {
                Button control = this.GetTemplateChild("PART_Bold") as Button;
                //Debug.Assert(control != null, "PART_Bold is not found the Template");
                return control;
            }
        }

        private Button Return
        {
            get
            {
                Button control = this.GetTemplateChild("PART_Return") as Button;
               // Debug.Assert(control != null, "PART_Return is not found the Template");
                return control;
            }
        }
        

        private TextBox TextField
        {
            get
            {
                TextBox control = this.GetTemplateChild("PART_Text") as TextBox;
                //Debug.WriteLine.Assert(control != null, "PART_Text is not found the Template");
                return control;
            }
        }
        
        #endregion

        #region Methods
        private void ApplyformatText(string format)
        {
            if (this.TextField != null)
            {
                if (!string.IsNullOrEmpty(this.TextField.SelectedText))
                {
                    int start = this.TextField.SelectionStart;

                    string text = string.Format(format, this.TextField.SelectedText);

                    string formatedText = this.TextField.Text.Substring(0, this.TextField.SelectionStart);

                    formatedText += text;

                    formatedText += this.TextField.Text.Substring(
                            this.TextField.SelectionStart + this.TextField.SelectedText.Length);
                    this.Text = formatedText;
                }
            }
        }

        private void InsertText(string text)
        {
            if (this.TextField != null)
            {
                int start = this.TextField.SelectionStart;

                string formatedText = this.TextField.Text.Substring(0, this.TextField.SelectionStart);

                formatedText += text;

                formatedText += this.TextField.Text.Substring(this.TextField.SelectionStart);
                this.Text = formatedText;
            }
           
        }

        private void SetText(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                textValue = value;
                if (this.TextField != null)
                {
                    if(this.TextField.Text!=value)
                        this.TextField.Text = value;
                }
            }
        }


        #endregion

        #region Events
        public override void OnApplyTemplate()
        {
            Paragraph.Click += new RoutedEventHandler(Paragraph_Click);
            Bold.Click += new RoutedEventHandler(Bold_Click);
            Return.Click += new RoutedEventHandler(Return_Click);

            TextField.TextChanged += new TextChangedEventHandler(TextField_TextChanged);
            
            SetText( textValue);
            base.OnApplyTemplate();
        }

        void TextField_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.Text=TextField.Text;
        }
       
        void Paragraph_Click(object sender, RoutedEventArgs e)
        {
            ApplyformatText("<p>{0}</p>");
        }

        void Bold_Click(object sender, RoutedEventArgs e)
        {
            ApplyformatText("<b>{0}</b>");
        }

        void Return_Click(object sender, RoutedEventArgs e)
        {
            InsertText("<br />");
        }

        private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((HtmlTextEditor)d).SetText((string)e.NewValue);
        }

       
        #endregion


      

        
    }
}
